package com.minhua.jijiehao2.mainframe.launcher.splashactivity.impl

import android.content.Intent
import android.view.animation.AlphaAnimation
import android.view.animation.Animation
import com.minhua.jijiehao2.R
import com.minhua.jijiehao2.base.BaseActivity
import com.minhua.jijiehao2.constants.DefineKey
import com.minhua.jijiehao2.mainframe.launcher.guideactivity.impl.GuideActivity
import com.minhua.jijiehao2.mainframe.launcher.splashactivity.imp.ISplashView
import com.minhua.jijiehao2.mainframe.main.impl.MainActivity
import com.minhua.jijiehao2.utils.SPUtils
import com.umeng.analytics.MobclickAgent
import kotlinx.android.synthetic.main.activity_splash.*

internal class SplashActivity : BaseActivity<ISplashView, SplashPresenter>(), ISplashView {
    companion object {
        var ANIMATION_DURATION = 2500L
    }

    private var isAnimationFinished = false
    private var isNetFinished = false

    override fun getLayoutId(): Int = R.layout.activity_splash

    override fun getPresenter(): SplashPresenter = SplashPresenter()

    override fun initView() {
        wvWeb.loadUrl("")
        mPresenter.getSwitchStatus()
        val animation = AlphaAnimation(0.5f, 1.0f)
        animation.duration = ANIMATION_DURATION
        animation.setAnimationListener(object : Animation.AnimationListener {
            override fun onAnimationRepeat(p0: Animation?) {

            }

            override fun onAnimationEnd(p0: Animation?) {
                isAnimationFinished = true
                if (isNetFinished) {
                    isAnimationFinished = false
                    startIntent()
                }
            }

            override fun onAnimationStart(p0: Animation?) {
            }
        })
        ivSplashContent.startAnimation(animation)
    }

    @Synchronized
    fun startIntent() {
        val firstRunApp: Boolean = SPUtils.get(DefineKey.FIRST_RUN_APP, true) as Boolean
        val switch: Int = SPUtils.get(DefineKey.SWITCH, 0) as Int
        if (firstRunApp) {
            if (switch == 1) {
                startActivity(Intent(this@SplashActivity, GuideActivity::class.java))
            } else {
                startActivity(Intent(this@SplashActivity, MainActivity::class.java))
            }
        } else {
            startActivity(Intent(this@SplashActivity, MainActivity::class.java))
        }
        this@SplashActivity.finish()
    }

    override fun onSwitchSuccess() {
        isNetFinished = true
        if (isAnimationFinished) {
            isNetFinished = false
            startIntent()
        }
    }

    override fun onBackPressed() {
        //屏蔽返回
        if (isAnimationFinished && !isNetFinished) {
            super.onBackPressed()
        }
    }

    override fun onResume() {
        super.onResume()
        MobclickAgent.onResume(this)
    }

    override fun onPause() {
        super.onPause()
        MobclickAgent.onPause(this)
    }
}
